package com.sgb.kjwl.viewmodel.self;

import androidx.databinding.ObservableField;

import android.util.Log;
import android.widget.Toast;



import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;

import com.sgb.kjwl.model.entity.LoginEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.businessmanagement.CompanyEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.swgk.core.event.EventConstant;

import com.sgb.kjwl.view.event.MainEvent;

import com.sgb.kjwl.view.ui.activity.self.SwitchingEnterprisesActivity;
import com.sgb.link.utils.Preferences;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SwSwitchingEnterprisesModel extends BaseViewModel {
    private SwitchingEnterprisesActivity activity;
    private SelfRepertory repertory;

    public ObservableField<UserMsgEntity> userInfo = new ObservableField<>();

    public SwSwitchingEnterprisesModel(SwitchingEnterprisesActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }
    public void loginOut() {
        DialogHelper.showProgressDialog(activity, null, "退出中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().logout().enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    Toast.makeText(activity, response.body().getData().toString(), Toast.LENGTH_SHORT);
                    JPushInterface.deleteAlias(activity, 1);
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, "");
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, "");
                    //TODO
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourcePCToken, "");
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourceAPPToken, "");

                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ENTERPRISE_AUTH, "");
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PERSON_AUTH, false);
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.DRIVER_AUTH, "");
                    repertory.saveToken(null);
//                    NimUIKit.setAccount("");
//                    AVChatKit.setAccount("");
                    Preferences.saveUserAccount("");
                    Preferences.saveUserToken("");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                repertory.saveToken(null);
//                NimUIKit.setAccount("");
//                AVChatKit.setAccount("");
                Preferences.saveUserAccount("");
                Preferences.saveUserToken("");
            }
        });
    }
    public void getUserInfo() {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) return;
                userInfo.set(response.body().getData());
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {

                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getCompanyList() {
        APIManager.getInstance().getMailListAPI().getCurrentCompany().enqueue(new Callback<BaseEntity<List<CompanyEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CompanyEntity>>> call, Response<BaseEntity<List<CompanyEntity>>> response) {
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    activity.showCompanyList(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CompanyEntity>>> call, Throwable t) {

            }
        });
    }
    public void settingMainCompany(CompanyEntity companyEntity) {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMailListAPI().settingMainCompany(companyEntity.getCompNo(),companyEntity.getType()).enqueue(new Callback<BaseEntity<LoginEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<LoginEntity>> call, Response<BaseEntity<LoginEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    //                    // 保存 token
                    LoginEntity entity=response.body().getData();
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourcePCToken, entity.getResourcePCToken());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourceAPPToken, entity.getResourceAPPToken());
                    //给SharedPreferenceUtil中的COMPANY_NO赋值
                    Log.e("公司no", companyEntity.getCompNo());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, companyEntity.getCompNo());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NAME, companyEntity.getCompName());
                    repertory.saveToken(entity.getToken());
                    activity.settingMainCompanysuccess();
                    MainEvent mainEvent = new MainEvent();
                    mainEvent.setMessageTag(EventConstant.EVENT_MAIN_ENTERPRISE_SWITCHING);
                    EventBus.getDefault().postSticky(mainEvent);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<LoginEntity>> call, Throwable t) {

            }
        });
    }
}
